package com.myorg.search;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.myorg.service.AdminService;
import com.myorg.service.StartupService;

@ManagedBean
@ViewScoped
public class AdminBean extends BaseBean implements Serializable {
	private static final long serialVersionUID = -6217255751833188075L;

	final static private Logger logger = LoggerFactory.getLogger(SearchBean.class);

	private boolean admin = false;

	private long unit = 1024 * 1024;

	private long fileSize = 1L;

	private int threadSize = 1;

	private String parentPath;

	@PostConstruct
	public void init() {
		HttpServletRequest origRequest = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
				.getRequest();
		admin = "true".equalsIgnoreCase(origRequest.getParameter("admin"));
	}

	public void run() {
		StartupService.touch();
		AdminService as = new AdminService();
		String msg = as.testDrive(threadSize, fileSize * unit, parentPath);
		addMessage("Success", "Test Drive took " + msg);
		logger.info("Done! " + msg);
	}

	public boolean isAdmin() {
		return admin;
	}

	public void setAdmin(boolean admin) {
		this.admin = admin;
	}

	public long getUnit() {
		return unit;
	}

	public void setUnit(long unit) {
		this.unit = unit;
	}

	public long getFileSize() {
		return fileSize;
	}

	public void setFileSize(long fileSize) {
		this.fileSize = fileSize;
	}

	public int getThreadSize() {
		return threadSize;
	}

	public void setThreadSize(int threadSize) {
		this.threadSize = threadSize;
	}

	public String getParentPath() {
		return parentPath;
	}

	public void setParentPath(String parentPath) {
		this.parentPath = parentPath;
	}
}
